/*
 * File: BaseCamera.cs
 * Author: Gourav Das
 * Purpose: Abstract class for Camera
 * Created: May 2011
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _22yards.ThreeD
{
    public abstract class BaseCamera:GameComponent
    {
        protected Vector3 LookAt;
        protected Vector3 position;
        private Matrix viewMatrix, projectionMatrix;
        private float fov = 45;

        protected Matrix rotationMatrix;
        protected Vector3 transformedReference;

        public float FOV
        {
            get
            {
                return fov;
            }
            protected set
            {
                fov = value;
            }
        }

        public Vector3 lookAt
        {
            get
            {
                return LookAt;
            }
        }

        public Vector3 Position
        {
            get
            {
                return position;
            }
            protected set
            {
                position = value;
            }
        }

        public Matrix ViewMatrix
        {
            get
            {
                return viewMatrix;
            }
            protected set
            {
                viewMatrix = value;
            }
        }

        public Matrix ProjectionMatrix
        {
            get
            {
                return projectionMatrix;
            }
            protected set
            {
                projectionMatrix = value;
            }
        }

        public virtual void ResetCamera()
        {
            position = new Vector3(0, 0, -1);
            LookAt = new Vector3(0, 0, 1);
            viewMatrix = Matrix.Identity;
            FOV = 45.0f;
            rotationMatrix = Matrix.CreateFromYawPitchRoll(0f, 0f, 0f);
            transformedReference = Vector3.Zero;
        }

        protected BaseCamera(Game _game)
            : base(_game)
        {
            ResetCamera();
        }

        public override void Update(GameTime _gametime)
        {
            if(FOV<1)
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(1), 16f / 9f, 0.1f, 1000f);
            else if(FOV>179)
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(179), 16f / 9f, 0.1f, 1000f);
            else
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(FOV), 16f / 9f, 0.1f, 1000f);
            viewMatrix = Matrix.CreateLookAt(position, LookAt, Vector3.Up);
        }

    }
}
